package com.itheima.service.db.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.UmsResource;
import com.itheima.domain.UmsRoleResourceRelation;
import com.itheima.mapper.UmsResourceMapper;
import com.itheima.mapper.UmsRoleResourceRelationMapper;
import com.itheima.service.db.UmsResoureService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ZhuHongKai
 * @version 1.0
 * @ClassName UmsResoureServiceImpl
 * @date 2022/11/13 2:07
 */
@DubboService
public class UmsResoureServiceImpl implements UmsResoureService {
    @Autowired
    UmsResourceMapper umsResourceMapper;

    @Autowired
    UmsRoleResourceRelationMapper umsRoleResourceRelationMapper;

    @Override
    public List<UmsResource> listResource(Long roleId) {
        LambdaQueryWrapper<UmsRoleResourceRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsRoleResourceRelation::getRoleId,roleId);
        List<UmsRoleResourceRelation> resourceRelations = umsRoleResourceRelationMapper.selectList(queryWrapper);
        List<UmsResource> umsResourceList=new ArrayList<>();
        if (CollUtil.isNotEmpty(resourceRelations)) {
            umsResourceList= resourceRelations.stream().map(
                    e -> {
                        Long resourceId = e.getResourceId();
                        LambdaQueryWrapper<UmsResource> qw = new LambdaQueryWrapper<>();
                        qw.eq(UmsResource::getId, resourceId);
                        UmsResource umsResource = umsResourceMapper.selectOne(qw);
                        return umsResource;

                    }
            ).collect(Collectors.toList());
        }

        return umsResourceList;
    }

    /**
     * 分页模糊查询后台资源
     *
     * @return
     */
    @Override
    public PageBeanVo getList(Map<String,String> map) {
        String categoryId = map.get("categoryId");
        String nameKeyword = map.get("nameKeyword");
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize = Integer.valueOf(map.get("pageSize"));
        String urlKeyword = map.get("urlKeyword");


        // 创建分页构造器
        Page<UmsResource> page = new Page<>(pageNum,pageSize);

        // 创建条件构造器
        LambdaQueryWrapper<UmsResource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(nameKeyword),UmsResource::getName,nameKeyword)
                .or().like(StrUtil.isNotEmpty(urlKeyword),UmsResource::getUrl,urlKeyword)
                .or().like(StrUtil.isNotEmpty(categoryId),UmsResource::getCategoryId,categoryId);
        Page<UmsResource> umsResourcePage = umsResourceMapper.selectPage(page, queryWrapper);


        return new PageBeanVo(pageNum,pageSize,umsResourcePage.getCurrent(),umsResourcePage.getTotal(), umsResourcePage.getRecords());
    }

    /**
     * 添加后台资源
     * @param umsResource
     */
    @Override
    public void create(UmsResource umsResource) {
        if (umsResource != null) {
            umsResource.setCreateTime(LocalDateTime.now());
            umsResourceMapper.insert(umsResource);
        }
    }

    /**
     * 修改后台资源
     * @param id
     * @param umsResource
     */
    @Override
    public void update(Long id, UmsResource umsResource) {
        umsResource.setId(id);
        umsResourceMapper.updateById(umsResource);
    }

    /**
     * 根据ID删除后台资源
     * @param id
     */
    @Override
    public void delete(Long id) {
        // 创建条件构造器
        LambdaQueryWrapper<UmsResource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsResource::getId,id);
        umsResourceMapper.delete(queryWrapper);
    }

    /**
     * 查询所有后台资源
     * @return
     */
    @Override
    public List<UmsResource> listAll() {
        List<UmsResource> umsResourceList = umsResourceMapper.selectList(null);
        return umsResourceList;
    }
}
